//
//  HWGameClassifyBannerCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/3.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import FSPagerView
class HWGameClassifyBannerCell: UITableViewCell {
    
    var banner: FSPagerView!
    
    var didClickBanner:((_ :String)->())?
    
    var model:HWClassifyModel? {
        didSet {
            banner.reloadData()
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameClassifyBannerCell) {
        let reUse:String = "HWGameClassifyBannerCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameClassifyBannerCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameClassifyBannerCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        banner = FSPagerView()
        banner.dataSource = self
        banner.delegate = self
        banner.isInfinite = true
        banner.register(HWGameClassifyBannerPagerCell.self, forCellWithReuseIdentifier: "HWGameClassifyBannerPagerCell")
        banner.automaticSlidingInterval = 3
        banner.removesInfiniteLoopForSingleItem = true
        contentView.addSubview(banner)
    }
    
    func initFrame() -> Void {
        banner.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview().priority(.high)
            make.height.equalTo(178*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWGameClassifyBannerCell:FSPagerViewDelegate,FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return model?.slide.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HWGameClassifyBannerPagerCell", at: index) as! HWGameClassifyBannerPagerCell
        cell.model = self.model?.slide[index]
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        self.didClickBanner?(model?.slide[index].gid ?? "")
    }
    
}

class HWGameClassifyBannerPagerCell:FSPagerViewCell {
    var gameImageView:UIImageView!
    var alphaView:UIView!
    var gameIconView:UIImageView!
    var nameLabel:UILabel!
    var starImageView:UIImageView!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var rightLabelBg:UIView!
    var rightLabel:UILabel!
    var textTagCollectionView:TTGTextTagCollectionView!
    
    var model:HWClassifyGameSlideModel?{
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.image ?? ""), completed: nil)
            gameIconView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.game_name
            scoreLabel.text = model?.point
            typeLabel.text = model?.type
            rightLabel.text = model?.name
            
            textTagCollectionView.removeAllTags()
            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 8*widthScale)
                
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 2*widthScale
                normalStyle.extraSpace = CGSize.init(width: 4*widthScale, height: 2*widthScale)
                normalStyle.backgroundColor = .white
                normalStyle.shadowColor = .white
                if idx == 0 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#F87706")
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FFEDDE")
                }else if idx == 1 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#EBAE38")
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FFF4DE")
                }else {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FF5D5D")
                    normalStyle.backgroundColor = .colorWithHexString(hexString: "#FFECEC")
                }
                
                //                normalStyle.shadowColor = UIColor.white
                //                normalStyle.extraSpace = CGSize.init(width: 5*widthScale, height: 7)
                
                let tag = TTGTextTag.init()
                tag.content = content
                //                tag.selectedContent = selectContent
                tag.style = normalStyle
                //                tag.selectedStyle = selectedStyle
                //                tag.selected = mod.isSelect
                
                textTagCollectionView.addTag(tag)
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        gameImageView = UIImageView()
        gameImageView.layer.cornerRadius = 10*widthScale
        gameImageView.layer.masksToBounds = true
        gameImageView.backgroundColor = .clear
        gameImageView.contentMode = .scaleAspectFill
        addSubview(gameImageView)
        
        alphaView = UIView()
        alphaView.frame = CGRect(x: 0, y: 0, width: 271*widthScale, height: 64*widthScale)
        alphaView.backgroundColor = .colorWithAlphaHexString(hexString: "#444444", alpha: 0.6)
        gameImageView.addSubview(alphaView)
//        UIColor.gradientColor(view: alphaView, colors: [UIColor.colorWithAlphaHexString(hexString: "#FFFFFF", alpha: 0).cgColor,
//                                                        UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 0, y: 1))
        
        gameIconView = UIImageView()
        gameIconView.layer.cornerRadius = 10*widthScale
        gameIconView.layer.masksToBounds = true
        gameIconView.backgroundColor = .clear
        gameIconView.contentMode = .scaleAspectFill
        gameImageView.addSubview(gameIconView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 14*widthScale)
        gameImageView.addSubview(nameLabel)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeStar")
        gameImageView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
        gameImageView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createBoldLabel(title: "", color: "FFFFFF", font: 10*widthScale)
        gameImageView.addSubview(typeLabel)
        
        rightLabelBg = UIView()
        rightLabelBg.layer.cornerRadius = 10*widthScale
        rightLabelBg.layer.borderWidth = 1*widthScale
        rightLabelBg.layer.borderColor = UIColor.white.cgColor
        gameImageView.addSubview(rightLabelBg)
        
        rightLabel = HWLabel.createBoldLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
//        rightLabel.layer.cornerRadius = 10*widthScale
//        rightLabel.layer.borderWidth = 1*widthScale
//        rightLabel.layer.borderColor = UIColor.white.cgColor
        rightLabel.textAlignment = .center
        rightLabelBg.addSubview(rightLabel)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self;
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left;
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        gameImageView.addSubview(textTagCollectionView)
        
    }
    
    private func initFrame() -> Void {
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.right.equalTo(-10*widthScale)
            make.height.equalTo(158*widthScale)
            make.centerY.equalToSuperview()
        }
        
        alphaView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(64*widthScale)
        }
        
        gameIconView.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.width.height.equalTo(52*widthScale)
            make.bottom.equalTo(-6*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(gameIconView.snp.right).offset(6*widthScale)
            make.top.equalTo(gameIconView).offset(4*widthScale)
        }
        
        starImageView.snp.makeConstraints { make in
            make.width.height.equalTo(8*widthScale)
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(4*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.left.equalTo(starImageView.snp.right)
            make.centerY.equalTo(starImageView)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(scoreLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(scoreLabel)
        }
        
        rightLabelBg.snp.makeConstraints { make in
            make.right.equalTo(-10*widthScale)
            make.height.equalTo(20*widthScale)
            make.centerY.equalTo(gameIconView)
        }
        rightLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(7*widthScale)
            make.right.equalTo(-7*widthScale)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(0)
            make.top.equalTo(starImageView.snp.bottom).offset(4*widthScale)
        }
    
    }
    
}

extension HWGameClassifyBannerPagerCell:TTGTextTagCollectionViewDelegate{
    @nonobjc func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {

        
    }
}
